<?php

function tblName($name=''){
	return TabName.$name;
}


/**
 * 生成插入数据SQL语句
 * 
 * @param String $tablename
 * @param Array $insertsqlarr
 * @param Integer $returnid
 */
function insertTableSql($tablename, $insertsqlarr, $replace=false) {
	
	$insertkeysql = $insertvaluesql = $comma = '';
	foreach ($insertsqlarr as $insert_key => $insert_value) {
		$insertkeysql .= $comma.'`'.$insert_key.'`';
		$insertvaluesql .= $comma.'\''.addslashes($insert_value).'\'';
		$comma = ', ';
	}
	
	$op = $replace?'REPLACE':'INSERT';
	
	$sql = $op.' INTO '.tblName($tablename).' ('.$insertkeysql.') VALUES ('.$insertvaluesql.')';
	
	return $sql;
}

/**
 * 生成修改数据SQL语句
 * 
 * @param $tablename
 * @param $updatesqlarr
 * @param $wheresqlarr
 */
function updateTableSql($tablename, $updatesqlarr, $wheresqlarr){
	
	$setsql = $comma = '';
	foreach ($updatesqlarr as $set_key => $set_value) {//fix
		$setsql .= $comma.'`'.$set_key.'`'.'=\''.addslashes($set_value).'\'';
		$comma = ', ';
	}
	$where = $comma = '';
	if(empty($wheresqlarr)) {
		$where = '1';
	} elseif(is_array($wheresqlarr)) {
		foreach ($wheresqlarr as $key => $value) {
			$where .= $comma.'`'.$key.'`'.'=\''.$value.'\'';
			$comma = ' AND ';
		}
	} else {
		$where = $wheresqlarr;
	}
	return 'UPDATE '.tblName($tablename).' SET '.$setsql.' WHERE '.$where ;
}

/**
 * 生成删除数据SQL语句
 * 
 * @param $tablename
 * @param $deletesqlarr
 */
function deleteTableSql($tablename, $deletesqlarr){
	
	$deleteWhereSql = $comma = '';
	foreach ($deletesqlarr as $delete_key => $delete_value) {
		$deleteWhereSql .= $comma.$deleteWhereSql;
		$deleteWhereSql = '`'.$delete_key.'` = \''.$delete_value.'\'';  
		$comma = ' AND ';
	}
	$sql = 'DELETE FROM '.tblName($tablename).' WHERE '.$deleteWhereSql;
	
	return $sql;
}


?>